package com.wm.wmim.server.mapstruct;

import com.wm.wmim.common.utils.MyCollectionUtils;
import com.wm.wmim.server.dto.bo.resp.ChatListRespBO;
import com.wm.wmim.server.dto.vo.resp.ChatListRespVO;
import com.wm.wmim.server.entity.ChatListDO;
import com.wm.wmim.server.entity.UserInfoDO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author weiming
 * @date 2024/9/2 23:06
 * @description
 */
@Mapper(componentModel = "spring")
public interface ChatListMapStruct {


    List<ChatListRespVO> fromBOToVO(List<ChatListRespBO> respBOS);


    @Mapping(target = "friendNickname", source = "userInfoDO.nickname")
    @Mapping(target = "friendAvatar", source = "userInfoDO.avatar")
    ChatListRespBO fromBOToVO(ChatListDO chatListDO, UserInfoDO userInfoDO);


    default List<ChatListRespBO> fromDOToBO(List<ChatListDO> chatListDOList, List<UserInfoDO> userInfoDOS) {
        Map<Long, UserInfoDO> map = MyCollectionUtils.toMap(userInfoDOS, UserInfoDO::getId);
        return chatListDOList.stream().map(item -> fromBOToVO(item, map.get(item.getFriendId()))).collect(Collectors.toList());
    }
}
